package br.ufmg.ubicomp.droidguide.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ufmg.ubicomp.droidguide.service.DGServerService;
import br.ufmg.ubicomp.droidguide.service.LoginService;
import br.ufmg.ubicomp.droidguide.util.DroidGuideFactory;

/**
 * Servlet implementation class for Servlet: DroidGuideEventServlet
 *
 */
 public class DroidGuideEventServlet extends ServletAbstract implements javax.servlet.Servlet {
   public static final String OPERATION = "operation";
static final long serialVersionUID = 1L;
   
	public DroidGuideEventServlet() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}   	
	
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    	
    	String operation = request.getParameter(OPERATION);
    	
    	if (operation == null)
    	{
    		writeToPage(request, response, "No operation defined");
    	} else	if (operation.equals("login"))
    	{
    		String username = request.getParameter(LoginService.USERNAME);
    		String password = request.getParameter(LoginService.PW);
    		log("process request: " + operation + " : " + 
    				username + " : " + password);
    		
    		DGServerService service = (DGServerService) getService(request, ServerServiceType.LOGIN);
    		
    		service.addParameter(LoginService.USERNAME, username);
    		service.addParameter(LoginService.PW, password);
    		
    		boolean success = service.execute();
    		writeToPage(request, response, success?"OK":"USER_NOT_FOUND");
    	}    	
    }
    
    private DGServerService getService(HttpServletRequest request, 
    		ServerServiceType type)
    {
    	DGServerService service = (DGServerService) getFromSession(request, type.toString());
    	
    	if (service == null)
    	{
    		switch (type)
    		{
    		case LOGIN:
    			service = new LoginService(DroidGuideFactory.createUsers());
    			addToSession(request, service, type);
    			break;
    		}
    	}
    	return service;
    }
    
    private void writeToPage(HttpServletRequest request, HttpServletResponse response, 
    		String message) throws IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
    	
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet NewServlet</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet at " + request.getContextPath () + "</h1>");
        println(out, "Remote Address: " + request.getRemoteAddr());
        println(out, "Remote Host   : " + request.getRemoteHost());
        println(out, "Renmote User  : " + request.getRemoteUser());
        println(out, "Remote Port   : " + request.getRemotePort());
        
        out.println("<h1>Message: " + message + "</h1>");        
        out.println("</body>");
        out.println("</html>");
    	
    }
    
    private void println(PrintWriter out, String message)
    {
    	out.println("<p>" + message + "</p>");
    }
}